$(function(){
	var $tabList=$("#tab_list"),$pagebar=$("#pagebar ul"),$pageInfo=$("#page_info"),
		pageNo=1,pageSize=10,count=0;//初始值
	/**---------------封装分页方法,count:总条目数,pageNo:当前页---------------*/
	function pageit(count){
		$pagebar.empty();
		//首页和上一页
		if(pageNo==1){
			$pagebar.append("<li id='start_page' class='disabled'><a href='javascript:void(0);'>首页</a></li>");
			$pagebar.append("<li id='prev_page'  class='disabled'><a href='javascript:void(0);'>上一页</a></li>");
		}else{
			$pagebar.append("<li id='start_page' pn=1><a href='javascript:void(0);'>首页</a></li>");
			$pagebar.append("<li id='prev_page' pn="+(pageNo-1)+"><a href='javascript:void(0);'>上一页</a></li>");
		}
		var tmp=parseInt(count/10);
		var page_size=count%10==0?(tmp):(tmp+1);//总页数
		//中间数字页
		if(page_size<=5){
			//如果小于5页,则全部显示
			for(var i = 0; i <page_size; i++) {
				if(i==pageNo-1){
					$pagebar.append("<li pn="+(i+1)+" class='active'><a href='javascript:void(0);'>"+(i+1)+"</a></li>");
				}else{
					$pagebar.append("<li pn="+(i+1)+"><a href='javascript:void(0);'>"+(i+1)+"</a></li>");
				}
			}
		}else{
			//如果总页数大于5页,显示当前页和前后各两页以及两个...符号
			if(pageNo<3){
				for(var i = 0; i <5; i++) {
					if(i==pageNo-1){
						$pagebar.append("<li pn="+(i+1)+" class='active'><a href='javascript:void(0);'>"+(i+1)+"</a></li>");
					}else{
						$pagebar.append("<li pn="+(i+1)+"><a href='javascript:void(0);'>"+(i+1)+"</a></li>");
					}
				}
				$pagebar.append("<li class='disabled'><a href='javascript:void(0);'>...</a></li>");
			}else if(pageNo>page_size-2){
				$pagebar.append("<li class='disabled'><a href='javascript:void(0);'>...</a></li>");
				for(var i = page_size-5; i <page_size; i++) {
					if(i==pageNo-1){
						$pagebar.append("<li pn="+(i+1)+" class='active'><a href='javascript:void(0);'>"+(i+1)+"</a></li>");
					}else{
						$pagebar.append("<li pn="+(i+1)+"><a href='javascript:void(0);'>"+(i+1)+"</a></li>");
					}
				}
			}else{
				for (var i = pageNo-3; i <pageNo+2; i++) {
					if(i==pageNo-1){
						$pagebar.append("<li pn="+(pageNo)+" class='active'><a href='javascript:void(0);'>"+(i+1)+"</a></li>");
					}else{
						$pagebar.append("<li pn="+(i+1)+"><a href='javascript:void(0);'>"+(i+1)+"</a></li>");
					}
				}
			}
		}
		//下一页和末页
		if(page_size==0||pageNo==page_size){
			$pagebar.append("<li id='next_page' class='disabled'><a href='javascript:void(0);'>下一页</a></li>");
			$pagebar.append("<li id='end_page' class='disabled'><a href='javascript:void(0);'>末页</a></li>");
		}else{
			$pagebar.append("<li id='next_page' pn="+(pageNo+1)+"><a href='javascript:void(0);'>下一页</a></li>");
			$pagebar.append("<li id='end_page' pn="+page_size+"><a href='javascript:void(0);'>末页</a></li>");
		}
		
		//绑定事件
		$pagebar.find("li").not(".disabled").on("click",function(){
			pageNo=parseInt($(this).attr("pn"));//更新当前页码
			reloadList();
		});
		//分页信息条
		$pageInfo.find("span").each(function(i,k){
			if(i==0){
				if(page_size==0){
					$(k).html(0);
				}else{
					$(k).html((pageNo-1)*10+1);
				}
			}else if(i==1){
				$(k).html(count>pageNo*10?pageNo*10:count);
			}else{
				$(k).html(count);
			}
		});
	}
	/**------------------加载列表---------------------*/
	var params={"pageNo":1,"pageSize":pageSize,"name":null,"phone":null};
	function reloadList(){
		params.pageNo=pageNo;
		params.pageSize=pageSize;
		params.name=$("#search_name").val();
		params.phone=$("#search_phone").val();
		$.get(serverURL+"user/list",params,function(data){
//			data=$.parseJSON(data);
			try {
				if(data.code==0){
					data=data.data;
					$tabList.find("tbody").empty();
					count=data.pagination.totalCount;
					if(count>0){
						var html="";
						$.each(data.list,function(i,k){
							html+="<tr>" +
							"<td id='pkc'><input type='checkbox' id='"+k.id+"' class='checkbox' col='"+i+"'></td>" +
							"<td>"+((pageNo-1)*10+i+1)+"</td>" +
							"<td>"+k.name+"</td>"+ 
							"<td>"+k.phone+"</td>"+ 
							"<td>"+k.coin+"</td>"+ 
							"<td>"+(parseInt(k.gender)==0?"女":"男")+"</td>"+ 
							"<td>"+(k.lastLoginTime?k.lastLoginTime:"尚未登录过")+"</td>"+ 
							"<td>"+k.registerTime+"</td>"+ 
							"</tr>";
						});
						$tabList.find("tbody").append(html);
//						$(".tools button").removeAttr("disabled");
					}else{
						$tabList.find("tbody").append("<tr><td colspan="+$("thead th").length+">无数据</td></tr>");
//						$(".tools button").not(".btn-default").attr("disabled","disabled");
					}
					pageit(count);//重绘工具栏
				}else if(data.code!=-1){
					alert("数据加载出错，请稍后重试");
				}
			} catch (e) {
				alert("数据加载出错，请稍后重试");
			}
		});
	}
	reloadList();
	//全选与取消全选
	$("#pkall").change(function(){
		if($(this).prop("checked")){
			$tabList.find("#pkc :checkbox").prop("checked",true);
		}else{
			$tabList.find("#pkc :checkbox").prop("checked",false);
		}
	});
	//获取选中的行
	function getSelectedCols(){
		return $tabList.find("#pkc :checkbox").filter(":checked");
	}
	/**---------------新增-----------------*/
	var $formAdd=$("#form_add"),$addName=$formAdd.find("[name=costcentName]"),$addNo=$formAdd.find("[name=costcentNo]"),
		$addScope=$formAdd.find("[name=costcentScope]"),$addDesc=$formAdd.find("[name=costcentDesc]");
	function checkAddForm(){
		if(!$addName.val()){
			warnInput($addName);
			return false;
		}
		if($addName.val().length>20){
			alert("名称不能超过20个字符");
			return false;
		}
		if(!$addNo.val()){
			warnInput($addNo);
			return false;
		}
		if($addNo.val().length>10){
			alert("编号不能超过10个字符");
			return false;
		}
		if($addScope.val().length>80){
			alert("功能范围不能超过80个字符");
			return false;
		}
		if($addDesc.val().length>80){
			alert("描述说明不能超过80个字符");
			return false;
		}
		return true;
	}
	$("#btn_confirm_add").click(function(){
		if(checkAddForm()){
			$.post(urls.webpath+"costcent/add",{"costcentName":$addName.val(),"costcentNo":$addNo.val(),
				"costcentScope":$addScope.val(),"costcentDesc":$addDesc.val()},function(data){
//				data=$.parseJSON(data);
				if(data.code==0){
					alert("添加成功");
					$("#modal_add").modal("hide");
					$formAdd.get(0).reset();
					reloadList();
				}else if(data.code!=-1){
					alert("添加失败，请稍后重试");
				}
			});
		}
	});
	/**------------------统计信息---------------------*/
	var $scanconsultNum=$("#scan_consultNum"),$scanfocusDocNum=$("#scan_focusDocNum"),$scanscoreTimes=$("#scan_scoreTimes"),$scanscoreTimesPercent=$("#scan_scoreTimesPercent"),
	    $scancashinTimes=$("#scan_cashinTimes"),$scancashinTotal=$("#scan_cashinTotal"),$scancostCoinNum=$("#scan_costCoinNum"),$scanappShareNum=$("#scan_appShareNum");
	$("#btn_count").click(function(){
		//获取选中的行		
		var $curCol=$tabList.find("#pkc :checkbox").filter(":checked");
    	if($curCol.length!=1){
    		alert("请选择一个用户！");
    		return ;
    	}
    	var id=$curCol[0].id;
    	//从后台获取该条记录所有信息
    	$.get(serverURL+"user/getMes",{"id":id},function(data){
    		if(data.code==0){
    			data=data.data;
    			$scanconsultNum.val(data.consultNum);
    			$scanfocusDocNum.val(data.focusDocNum);
    			$scanscoreTimes.val(data.scoreTimes);
    			if(data.consultNum!=0){
    				var num=data.scoreTimes/data.consultNum;
    				$scanscoreTimesPercent.val(Math.round(num*100)+"%");
    			}else{
    				$scanscoreTimesPercent.val(0);
    			}
    			$scancashinTimes.val(data.cashinTimes);
    			$scancashinTotal.val(data.cashinTotal);
    			$scancostCoinNum.val(data.costCoinTotal);
    			$scanappShareNum.val(data.appShareNum);
    			$("#modal_census").modal("show");
    		}else if(data.code!=-1){
    			alert("信息加载失败");
    		}
    	});
    });
	/**---------------删除-----------------
	$("#btn_del").click(function(){
		new $.zui.Messager('该功能是否需要还待确定！', {placement: 'top',type:'warning'}).show();
	});
	*/
	/**---------------查询-----------------*/
	$("#btn_search").click(function(){
		params.name=$("#search_name").val();
		params.phone=$("#search_phone").val();
		pageNo=1;
		reloadList();
		return false;
	});
	/**---------------重置-----------------*/
	$("#btn_reset").click(function(){
		$("#form_search").get(0).reset();
		pageNo=1;
		reloadList();
		return false;
	});
});